package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.common.util.CommonUtils;
import com.augurit.gzcgwsp.csshlj.form.ShljYsForm;
import com.augurit.gzcgwsp.csshlj.service.ICsshljCzService;
import com.augurit.gzcgwsp.csshlj.service.IShljYsService;
import com.augurit.gzcgwsp.excelimportexport.util.ExcelUtil;
import com.augurit.gzcgwsp.sendNotice.service.IYwMtbjlshInfoService;
import com.augurit.gzcgwsp.smartformCsdlwj.convert.YwHwShljqsConvertor;
import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwShljqsDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwShljqs;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwSrCsdlwj;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwShljqsService;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwLyqsclqksmService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.ShljqsForm;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjclService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import com.augurit.wf.app.doccode.service.IOaDocCodeService;
import com.augurit.wf.app.doccode.web.form.OaDocCodeForm;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

/**
 * 目前这个类，只有与方欣的数据交换用到
 * @author Administrator
 *
 */

@Service
@Transactional
public class YwShljqsServiceImpl extends WfBusClientServiceImpl<ShljqsForm> implements IYwShljqsService{

	@Autowired
	private YwHwShljqsDao ywHwShljqsDao;
    @Autowired
    private IOaDocCodeService doccodeService;
    @Autowired
    private IYwComQyxxService ywComQyxxService;
    @Autowired
    private IYwComSjclService sjclService;
    @Autowired
	private IYwComSjcllistService ywComSjcllistService;
	@Autowired
	private IYwComSjclService ywComSjclService;
	@Autowired
	private ICsshljCzService csshljCzService;
	@Autowired
	private IShljYsService shljYsService;
	@Autowired
	private IYwHwLyqsclqksmService ywHwLyqsclqksmService;
	@Autowired
    private IYwMtbjlshInfoService ywMtbjlshInfoService;
	
	/**
	 * ��������ȡForm����
	 */
	@Transactional(readOnly = true)
	public ShljqsForm get(Long id) {
		YwHwShljqs entity = ywHwShljqsDao.get(id);
		ShljqsForm form = YwHwShljqsConvertor.convertVoToForm(entity);
		if(StringUtils.isNotBlank(form.getQybh())){
		       YwComQyxxForm qyxxform = ywComQyxxService.get(Long.parseLong(form.getQybh()));
		       if(qyxxform!=null){
		    	    YwHwShljqsConvertor.convertQyxxFormToForm(qyxxform, form);
			        form.setSqdw(qyxxform.getSqdw());
		       }
		    
		}
 
      
        return form;
        //return YwHwShljqsConvertor.convertVoToForm(entity);
	}
	
	//通过案件号获得实体类
    public List<YwHwShljqs> getByAcceptNo(String acceptNo) {
    	String sql = "from YwHwShljqs t where t.acceptNo='" + acceptNo + "'";
        List<YwHwShljqs> entityList = ywHwShljqsDao.find(sql);
        return entityList;
    }
	
    public void changeYslState(Long id,Long state) {
//    	ShljqsForm form = this.get(id);
//    	form.setYsl(state);
//    	this.save(form);
    	YwHwShljqs entity = ywHwShljqsDao.get(id);
    	entity.setYsl(state);
    	ywHwShljqsDao.save(entity);
    }
    
    //是否延期办理
    public void changeIsycbl(Long id, String state) {
    	YwHwShljqs entity = ywHwShljqsDao.get(id);
//    	entity.setIsycbl(state);
    	ywHwShljqsDao.save(entity);
	}
    
    /**
     * 设置该案件为咨询服务案件
     * @param id
     */
    public void changeIsZxfwaj(Long id) {
    	YwHwShljqs entity = ywHwShljqsDao.get(id);
//    	entity.setIsZxfwaj("Y");
    	ywHwShljqsDao.save(entity);
    }
	
    //保存结案时间
	public void endSave(Long id) {
		YwHwShljqs entity = ywHwShljqsDao.get(id);
    	entity.setJarq(new Date());
    	ywHwShljqsDao.save(entity);
	}
    
	public void delete(Long... ids) {
		ywHwShljqsDao.delete(ids);
	}

	/**
	 * �����������޸ĵ�Form�����б�
	 */
	public void save(ShljqsForm... forms) {
		if(forms != null)
			for(ShljqsForm form : forms)
				this.save(form);
	}
	
	/**
	 * �����������޸ĵ�Form����
	 */
	public void save(ShljqsForm form){
		System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"+form.getSqdw());
		
        if (form == null) return;

		YwHwShljqs entity = new YwHwShljqs(); 
        if(form != null && form.getId() != null){
            entity = ywHwShljqsDao.get(form.getId());
            if (StringUtils.isBlank(entity.getAcceptNo()) || form.getAcceptNo().indexOf("XXXX") > 0) form.setAcceptNo(null);
        }else{
            entity = new YwHwShljqs(); 
            form.setAcceptNo(null);
        }
        
      //保存企业信息
        YwComQyxxForm qyxxForm = new YwComQyxxForm();
        if(StringUtils.isEmpty(form.getQybh())){
        	if(StringUtils.isNotBlank(form.getSqdw())){
            	YwComQyxxForm ywComQyxxForm = ywComQyxxService.getFormBySqdw(form.getSqdw(), "shljqssj");
            	if(ywComQyxxForm != null){
            		qyxxForm = ywComQyxxForm;
            	}
            }
        }
        YwHwShljqsConvertor.convertFormToQyxxForm(form, qyxxForm);
        qyxxForm.setTemplateCode("shljqssj");
        ywComQyxxService.update(qyxxForm);
        form.setQybh(qyxxForm.getId().toString());
        System.out.println("&&&&&&&&&&&&&&&&&&&&&  qyxxForm =  "+qyxxForm.getId());
        
        boolean isNewForm = false;
        if (StringUtils.isBlank(form.getAcceptNo())) {
        	//生成政务中心统一查询编号 开始
			Map<String,String> serialNoAndPasswordMap = null;
        	if("Y".equals(form.getSendNotice())){//网办，1
        		serialNoAndPasswordMap = ywMtbjlshInfoService.getSerialNoAndPassword("1");
        	}else{//大厅，0
        		serialNoAndPasswordMap = ywMtbjlshInfoService.getSerialNoAndPassword("0");
        	}
        	String serialNo = serialNoAndPasswordMap.get("serialNo");
        	String password = serialNoAndPasswordMap.get("password");
			form.setSerialNo(serialNo);
			form.setPassword(password);
			form.setAcceptNo(serialNo);
    		//生成政务中心统一查询编号 结束
            isNewForm = true;
        }
        YwHwShljqsConvertor.convertFormToVo(form, entity); 
        System.out.println("&&&&&&&&&&&&&&&&&&&&&  后  "+entity.getSqdw());
        System.out.println("&&&&&&&&&&&&&&&&&&&&&  entity.getId() =  "+entity.getId());
        Session session = ywHwShljqsDao.getSessionFactory().openSession();
        session.save(entity);
        session.flush();
        session.close();
        form.setId(entity.getId());
        
        if (isNewForm) {
            sjclService.updateByNewForm(form.getId());
        }
	}

	/**
	 * ���Form����Ĳ�ѯ��������ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<ShljqsForm> search(Page<ShljqsForm> page, ShljqsForm form) {
		// �����µķ�ҳ����
		Page pg = PageUtils.newPageInstance(page);
		
		// ��ѯ��估����
		StringBuffer hql = new StringBuffer("from YwHwShljqs ps where 1=1");
		List values = new ArrayList();
		
		// ��ѯ����
		if(form != null){
			
		}
		
		//����
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// ִ�з�ҳ��ѯ����
		ywHwShljqsDao.findPage(pg, hql.toString(), values);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		List<ShljqsForm> list = YwHwShljqsConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * ��ݹ��������б�����ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<ShljqsForm> search(Page<ShljqsForm> page, List<PropertyFilter> filters) {
		// �����µķ�ҳ����
		Page<YwHwShljqs> pg = PageUtils.newPageInstance(page);
		
		// ������������ҳ���Ҷ���
		ywHwShljqsDao.findPage(pg, filters);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		List<ShljqsForm> list = YwHwShljqsConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 通过ID号,改变窗口是否可打印受理通知书状态(1为是；0和空为否)
	 * @param acceptNo 受理号
	 * @return Page<YwRqSsgdSqb>
	 */
	public void changeSendAcceptNotice(Long id){
		YwHwShljqs entity = ywHwShljqsDao.get(id);
//		entity.setSendAcceptNotice("1");
		ywHwShljqsDao.save(entity);
	}
	
	/**
	 * 通过ID号,改变窗口是否可打印延期办理结果通知书状态(1为是；0和空为否)
	 * @param id 
	 * @return 
	 */
	public void changeSendYqNotice(Long id){
		YwHwShljqs entity = ywHwShljqsDao.get(id);
//		entity.setSendYqNotice("1");
		ywHwShljqsDao.save(entity);
	}
	
	/**
	 * 根据ID获得对象实体
	 * @param id
	 * @return
	 */  
	public YwHwShljqs getEntity(Long id){
		return ywHwShljqsDao.get(id);
	}


	public List getListById2(Long id,List list){
	    String sql="from YwHwShljqs ps where id="+id;
        return ywHwShljqsDao.find(sql,list);
    }
	/**
     * 保存对象实体
     * @param ywSrDlzy
     */     
    public void saveEntity(YwHwShljqs ywSrDlzy){
    	ywHwShljqsDao.save(ywSrDlzy);
    }


	public List getListById(Long id){
        String sql="select * from HW_SHLJQS_V1 where id="+id;
        return ywHwShljqsDao.getListById(sql);
    }
	
	/**
     * 保存政务中心的统一查询编号和密码
     * @param id
     * @param serialNo
     * @param password
     */
    public void saveSerialNoAndPassword(Long id,String serialNo,String password) {
    	YwHwShljqs entity = ywHwShljqsDao.get(id);
    	entity.setSerialNo(serialNo);
    	entity.setPassword(password);
    	ywHwShljqsDao.save(entity);
    }
}
